//
//  HEHealthRankCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/8.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthRankCell.h"

@interface HEHealthRankCell ()

@property (nonatomic, strong) UILabel *rankLb;
@property (nonatomic, strong) UIImageView *owrHeadView;
@property (nonatomic, strong) UILabel *distanceLb;
@property (nonatomic, strong) UILabel *owrNameLb;
@property (nonatomic, strong) UILabel *scoreLb;
@property (nonatomic, strong) QMUIButton *likeBtn;

@end

@implementation HEHealthRankCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.qmui_borderPosition = QMUIViewBorderPositionTop;
        self.qmui_borderWidth = 0.5;
        self.qmui_borderColor = CSplitLineColor;
     
        [self initUI];
    }
    return self;
}

- (void)initUI {
   
    // 排名
    UILabel *rankLb = [UILabel new];
    rankLb.font = DYFont(16);
    rankLb.text = @"0";
    [self.contentView addSubview:rankLb];
    _rankLb = rankLb;
    
    // 头像
    UIImageView *owrHeadView = [UIImageView new];
    owrHeadView.image = IMAGE_NAMED(@"defualt_icon");
    owrHeadView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:owrHeadView];
    owrHeadView.layer.cornerRadius = 25;
    owrHeadView.layer.masksToBounds = YES;
    _owrHeadView = owrHeadView;
    
    // 姓名
    UILabel *owrNameLb = [UILabel new];
    owrNameLb.font = DYBlodFont(16);
    owrNameLb.text = @"-";
    [self.contentView addSubview:owrNameLb];
    _owrNameLb = owrNameLb;
    
    // 距离
    UILabel *distanceLb = [UILabel new];
    distanceLb.font = DYFont(16);
    distanceLb.text = @"0km";
    distanceLb.textColor = CNavDargGrayColor;
    [self.contentView addSubview:distanceLb];
    _distanceLb = distanceLb;
 
    // 分数
    UILabel *scoreLb = [UILabel new];
    scoreLb.font = DYBlodFont(18);
    scoreLb.text = @"0";
    scoreLb.textColor = KHexColor(@"FFA900");
    [self.contentView addSubview:scoreLb];
    _scoreLb = scoreLb;
    
    // 喜欢按钮
    QMUIButton *likeBtn = [[QMUIButton alloc] init];
    likeBtn.imagePosition = QMUIButtonImagePositionBottom;
    likeBtn.tintColorAdjustsTitleAndImage = CNavSubTitleColor;
    likeBtn.adjustsImageTintColorAutomatically = NO;
    likeBtn.spacingBetweenImageAndTitle = 0;
    [likeBtn setImage:UIImageMake(@"like_n") forState:UIControlStateNormal];
    [likeBtn setTitle:@"1" forState:UIControlStateNormal];
    likeBtn.titleLabel.font = DYFont(15);
    [self.contentView addSubview:likeBtn];
    [likeBtn addTarget:self action:@selector(checkLikeClick) forControlEvents:UIControlEventTouchUpInside];
    _likeBtn = likeBtn;
    
    
    [rankLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.mas_offset(30);
    }];
    
    [owrHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(10);
        make.left.equalTo(rankLb.mas_right).mas_offset(15);
        make.width.mas_offset(50);
        make.height.mas_offset(50);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
    
    [owrNameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(owrHeadView.mas_top);
        make.left.equalTo(owrHeadView.mas_right).mas_offset(15);
    }];
    
    [distanceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(owrHeadView.mas_bottom);
        make.left.equalTo(owrHeadView.mas_right).mas_offset(15);
    }];
    
    [scoreLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(likeBtn.mas_left).mas_offset(-15);
    }];
    [scoreLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)setModel:(HEHealthRankModel *)model {
    _model = model;
    
    _rankLb.text = model.rank_nbr;
    [_owrHeadView sd_setImageWithURL:[NSURL URLWithString:model.user_icon] placeholderImage:IMAGE_NAMED(@"defualt_icon")];
    _owrNameLb.text = [NSString stringWithFormat:@"%@", model.usname];
    _scoreLb.text = [NSString stringWithFormat:@"%.2f", model.health_value];
    [_likeBtn setTitle: model.thumbup forState:UIControlStateNormal];
    _distanceLb.text = [NSString stringWithFormat:@"%.2fkm", model.distance];
    
    [self refreshTh];
}

- (void)refreshTh {
    if (_model.okThumbup) {
        [_likeBtn setImage:UIImageMake(@"like_s") forState:UIControlStateNormal];
    }else {
        [_likeBtn setImage:UIImageMake(@"like_n") forState:UIControlStateNormal];
    }
}

- (void)checkLikeClick {
    
    if ([_delegate respondsToSelector:@selector(cellWithThumbupTap:)]) {
        [_delegate cellWithThumbupTap: _model];
    } 
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
